package com.itheima.mq;

import com.itheima.health.service.OrderService;
import com.itheima.health.utils.sms.SmsUtils;
import com.rabbitmq.client.BasicProperties;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.MessageProperties;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

@Component
public class SmsConsumer {

    @RabbitListener(queues = "lws-queue")
    public void receive(Map map, Channel channel, Message message) throws IOException {
        long deliveryTag=0L;
        try {
            //会话唯一ID
            deliveryTag  = message.getMessageProperties().getDeliveryTag();
//            System.out.println(1/0);
            String telephone = (String)map.get("telephone");
            String code = (String)map.get("code");
            System.out.println(code+"----"+telephone);
            //  SMSUtils.sendShortMessage(telephone,code);
            //  手动处理ack  通知broker 删除当前消息（可以设置批量删除）
            channel.basicAck(deliveryTag,false);
        }catch (Exception e){
            e.printStackTrace();
            channel.basicNack(deliveryTag,false,true);//  第三个参数 ： true  表示 出现代码业务一次 消息 重新放回队列中 再次获取
        }
    }
}
